#include "fuDateTime.h"
//#include <libs/date_time/src/gregorian/greg_month.cpp>

BEGIN_NAMESPACE_CPPX

//////////////////////////////////////////////////////////////////////////
fuTimeDiff::fuTimeDiff( boost::posix_time::time_duration diff )
: m_diff(diff)
{

}

fuSecond_t fuTimeDiff::toSeconds( void ) const
{
    return m_diff.total_seconds();
}

fuTimeTick_t fuTimeDiff::toMilliSeconds( void ) const
{
    return m_diff.total_milliseconds();
}

fuTimeTick_t fuTimeDiff::toMicroSeconds( void ) const
{
    return m_diff.total_microseconds();
}

//////////////////////////////////////////////////////////////////////////
fuDateTime::fuDateTime( void )
{

}

fuDateTime::fuDateTime( std::string time )
{
    fromString(time);
}

fuDateTime::fuDateTime( boost::posix_time::ptime time )
{
    m_time = time;
}

fuDateTime::operator boost::posix_time::ptime( void ) const
{
    return m_time;
}

boost::posix_time::ptime fuDateTime::get( void ) const
{
    return m_time;
}

fuDateTime fuDateTime::now( void )
{
    boost::posix_time::ptime time(boost::posix_time::microsec_clock::local_time());
    return fuDateTime(time);
}

fuDateTime fuDateTime::begin( void )
{
    boost::posix_time::ptime time(boost::date_time::min_date_time);
    return fuDateTime(time);
}

fuDateTime fuDateTime::end( void )
{
    boost::posix_time::ptime time(boost::date_time::max_date_time);
    return fuDateTime(time);
}

fuDateTime & fuDateTime::fromString( std::string str )
{
    m_time = boost::posix_time::time_from_string(str);
    return (*this);
}

std::string fuDateTime::toString( format_t fmt /*= DT_FULL*/ ) const
{
    using boost::posix_time::time_facet;
    std::stringstream ss;
    cstr fmt_s =
        (fmt == DT_FULL)   ? "%Y-%m-%d %H:%M:%S%F" :
        (fmt == DT_SECOND) ? "%Y-%m-%d %H:%M:%S" :
        (fmt == DT_MINUTE) ? "%Y-%m-%d %H:%M" :
        (fmt == DT_HOUR)   ? "%Y-%m-%d %H" :
        (fmt == DT_DAY)    ? "%Y-%m-%d" :
        (fmt == DT_MONTH)  ? "%Y-%m" :
        (fmt == DT_YEAR)   ? "%Y" : "";
    ss.imbue(std::locale(std::cout.getloc(),new time_facet(fmt_s)));
    ss << m_time;
    return ss.str();
}

fuSecond_t fuDateTime::toSeconds( void ) const
{
    boost::posix_time::time_duration dura = m_time - begin().get();
    return dura.total_seconds();
}

fuTimeTick_t fuDateTime::toMilliSeconds( void ) const
{
    boost::posix_time::time_duration dura = m_time - begin().get();
    return dura.total_milliseconds();
}

fuTimeTick_t fuDateTime::toMicroSeconds( void ) const
{
    boost::posix_time::time_duration dura = m_time - begin().get();
    return dura.total_microseconds();
}

fuTimeDiff fuDateTime::operator-( fuDateTime const & value )
{
    boost::posix_time::time_duration dura = m_time - value.m_time;
    return fuTimeDiff(dura);
}

bool fuDateTime::operator<( fuDateTime const & value ) const
{
    return m_time < value.m_time;
}

bool fuDateTime::operator>( fuDateTime const & value ) const
{
    return m_time > value.m_time;
}

END_NAMESPACE_CPPX
